﻿#Region "IMPORT`S"

Imports Microsoft.VisualBasic _
      , System.Drawing.Imaging _
      , System.Windows.Forms _
      , System.Web.Caching _
      , System.Threading _
      , System.Drawing _
      , System.Net _
      , System.Web _
      , System.IO _
      , System

#End Region

Public Class ICORE_9 : Implements IHttpHandler

    Public Class WebsiteThumbnail

        Public [SCR] As Bitmap = Nothing _
               , URI As String = Nothing

        Sub New(ByVal T As String)
            Me.URI = T
        End Sub

        Public Function PROCESSING() As Bitmap
            [SCR] = Nothing
            Try
                Dim _Thread As Thread _
                        = New Thread(New ThreadStart(AddressOf GetThumbnail))
                _Thread.SetApartmentState(ApartmentState.STA)
                _Thread.Start()
                For ID As Integer = 1 To 100
                    If Not IsNothing([SCR]) Then
                        Application.DoEvents() _
                                  : Thread.Sleep(2000)
                        Exit For
                    End If
                    Application.DoEvents() _
                              : Thread.Sleep(150)
                Next
                If _Thread.ThreadState = Threading.ThreadState.Running Then
                    _Thread.Abort()
                End If
            Catch ex As Exception
            End Try
            Return [SCR]
        End Function

        Protected Sub GetThumbnail()
            Using [Web] As WebBrowser = New WebBrowser
                [Web].ClientSize = New Size(1024, 768)
                [Web].ScrollBarsEnabled = False
                [Web].ScriptErrorsSuppressed = True
                [Web].Navigate(URI)
                While [Web].ReadyState <> WebBrowserReadyState.Complete
                    Application.DoEvents() _
                              : Thread.Sleep(100)
                End While
                [SCR] = New Bitmap(1024, 768)
                [Web].DrawToBitmap([SCR], New Rectangle(0, 0, 1024, 768))
                [Web].Dispose()
            End Using
        End Sub

    End Class

    Function CheckBid(ByRef context As HttpContext) As String
        ' 
        Dim bid As String = context.Request.Url.ToString _
          , value As String = Nothing
        Try
            Dim arr As String() _
                    = bid.Split("?") _
              , ID As Integer = 0
            For Each str As String In arr
                Select Case ID
                    Case 0
                        '
                        '
                    Case 1
                        value = String.Concat(value, str.Substring(4, str.Length - 4))
                    Case Else
                        value = String.Concat(value, "?", str)
                End Select
                ID += 1
            Next
        Catch ex As Exception
        End Try
        '
        Return value
        ''
    End Function

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        '
        Dim bmp As Bitmap _
                = New Bitmap(145, 114) _
          , G As Graphics _
                = Graphics.FromImage(bmp) _
          , URI As String _
                = Nothing
        Try
            URI = CheckBid(context).Split("|")(0).ToLower
        Catch ex As Exception
        End Try
        Try
            If Not IsNothing(URI) Then
                '-----------{
                Dim [_tmp] As Bitmap _
                        = THUMBNAILS.BinaryAssembly._GET(URI)
                If IsNothing([_tmp]) Then
                    Try
                        Dim [tmp] As Bitmap = New WebsiteThumbnail(URI).PROCESSING()
                        Using [ITextureBrush] As TextureBrush _
                                           = New TextureBrush([tmp], New Rectangle(0, 0, 1024, 768))
                            ITextureBrush.ScaleTransform(0.17499999999999999, 0.16499999999999998, Drawing2D.MatrixOrder.Append)
                            G.FillRectangle(ITextureBrush, New Rectangle(0, 0, 145, 114))
                            ITextureBrush.Dispose()
                        End Using
                        [tmp].Dispose()
                        G.Dispose()

                        THUMBNAILS.BinaryAssembly._SET(URI, bmp)
                        THUMBNAILS.BinaryAssembly.SAVE()

                    Catch ex As Exception
                        'Windows.Forms.MessageBox.Show(ex.Message )
                    End Try
                Else
                    bmp = [_tmp]
                End If
                context.Response.ContentType = "image/jpeg"
                bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
                '}------
            End If
        Catch ex As Exception
        End Try
        '
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

End Class

'ALTERNAT`IVE:

'#Region "IMPORT`S"

'Imports System.Runtime.InteropServices _ 
'      , System.Runtime.CompilerServices _
'      , Microsoft.VisualBasic _
'      , System.Drawing.Imaging _
'      , System.ComponentModel _
'      , System.Windows.Forms _
'      , System.Web.Caching _
'      , System.Threading _
'      , System.Drawing _
'      , System.Net _
'      , System.Web _
'      , System.IO _
'      , System

'#End Region

'Public Class ICORE_9 : Implements IHttpHandler

'    Public Class WebsiteThumbnail

'        <ComImport(), _
'         ComVisible(True), _
'         Guid("0000010D-0000-0000-C000-000000000046"), _
'         InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
'        Public Interface IViewObject
'            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
'            Sub Draw(<MarshalAs(UnmanagedType.U4)> ByVal dwAspect As UInt32 _
'                                                     , ByVal lindex As Integer _
'                                                     , ByVal pvAspect As IntPtr _
'                                              , <[In]()> ByVal ptd As IntPtr _
'                                                     , ByVal hdcTargetDev As IntPtr _
'                                                     , ByVal hdcDraw As IntPtr _
'                   , <MarshalAs(UnmanagedType.Struct)> ByRef lprcBounds As Rectangle _
'                                              , <[In]()> ByVal lprcWBounds As IntPtr _
'                                                     , ByVal pfnContinue As IntPtr _
'                       , <MarshalAs(UnmanagedType.U4)> ByVal dwContinue As UInt32)
'        End Interface

'        Public [SCR] As Bitmap = Nothing _
'               , URI As String = Nothing _
'               , [F] As Boolean = False

'        Sub New(ByVal T As String)
'            Me.URI = T
'        End Sub

'        Public Function PROCESSING() As Bitmap
'            '320, 240
'            [SCR] = New Bitmap(246, 184)
'            Try
'                Dim _Thread As Thread _
'                        = New Thread(New ThreadStart(AddressOf GetThumbnail))
'                _Thread.SetApartmentState(ApartmentState.STA)
'                _Thread.Start()
'                _Thread.Join()
'            Catch ex As Exception
'            End Try
'            Return [SCR]
'        End Function

'        Protected Sub GetThumbnail()
'            Using [Web] As WebBrowser = New WebBrowser
'                Try
'                    [Web].ClientSize = New Size(1024, 768)
'                    [Web].ScrollBarsEnabled = False
'                    [Web].ScriptErrorsSuppressed = True
'                    AddHandler [Web].NewWindow _
'                            , New CancelEventHandler(AddressOf [NEW])
'                    AddHandler [Web].DocumentCompleted _
'                            , New WebBrowserDocumentCompletedEventHandler(AddressOf [COMPLETED])
'                    [Web].Navigate(URI)
'                    Dim IDD As Integer = 0
'                    '[Web].ReadyState <> WebBrowserReadyState.Complete
'                    While [F] = False
'                        Application.DoEvents() _
'                                  : Thread.Sleep(200)
'                        If IDD > 100 Then
'                            Exit While
'                        End If
'                        IDD += 1
'                    End While
'                    '
'                    Using G As Graphics = Graphics.FromImage([SCR])
'                        G.Clear(Color.Black)
'                        Dim [ptr] As IntPtr = Nothing
'                        [ptr] = G.GetHdc
'                        Dim rect As New Rectangle(0, 0, [SCR].Width, [SCR].Height)
'                        Try
'                            TryCast([Web].Document.DomDocument, IViewObject).Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, [ptr], rect, IntPtr.Zero, IntPtr.Zero, 0)
'                        Catch ex As Exception
'                        End Try
'                        If ([ptr] <> IntPtr.Zero) Then
'                            G.ReleaseHdc([ptr])
'                        End If
'                    End Using
'                Catch ex As Exception
'                End Try
'                [Web].Dispose()
'            End Using
'        End Sub

'        Protected Sub [New](ByVal [obj] As Object, ByVal e As CancelEventArgs)
'            e.Cancel = True
'        End Sub
'        Protected Sub [COMPLETED](ByVal [obj] As WebBrowser, _
'            ByVal e As WebBrowserDocumentCompletedEventArgs)
'            Application.DoEvents() _
'                : Thread.Sleep(2500)
'            [obj].Stop()
'            [F] = True
'        End Sub

'    End Class

'    Function CheckBid(ByRef context As HttpContext) As String
'        ' 
'        Dim bid As String = context.Request.Url.ToString _
'          , value As String = Nothing
'        Try
'            Dim arr As String() _
'                    = bid.Split("?") _
'              , ID As Integer = 0
'            For Each str As String In arr
'                Select Case ID
'                    Case 0
'                        '
'                        '
'                    Case 1
'                        value = String.Concat(value, str.Substring(4, str.Length - 4))
'                    Case Else
'                        value = String.Concat(value, "?", str)
'                End Select
'                ID += 1
'            Next
'        Catch ex As Exception
'        End Try
'        '
'        Return value
'        ''
'    End Function

'    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
'        '
'        Dim bmp As Bitmap _
'                = New Bitmap(145, 114) _
'          , G As Graphics _
'                = Graphics.FromImage(bmp) _
'          , URI As String _
'                = Nothing
'        Try
'            URI = CheckBid(context).Split("|")(0).ToLower
'        Catch ex As Exception
'        End Try
'        Try
'            If Not IsNothing(URI) Then
'                '-----------{
'                Dim [_tmp] As Bitmap _
'                        = THUMBNAILS.BinaryAssembly._GET(URI)
'                If IsNothing([_tmp]) Then
'                    Try
'                        bmp = New WebsiteThumbnail(URI).PROCESSING()
'                        THUMBNAILS.BinaryAssembly._SET(URI, bmp)
'                        THUMBNAILS.BinaryAssembly.SAVE()
'                    Catch ex As Exception
'                        'Windows.Forms.MessageBox.Show(ex.Message )
'                    End Try
'                Else
'                    bmp = [_tmp]
'                End If
'                context.Response.ContentType = "image/jpeg"
'                bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
'                '}------
'            End If
'        Catch ex As Exception
'        End Try
'        '
'    End Sub

'    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
'        Get
'            Return True
'        End Get
'    End Property

'End Class
